package encomendaFacil.impl;

import java.util.Map;

import encomendaFacil.bean.User;
import encomendaFacil.dao.ColumnIndexes;
import encomendaFacil.dao.DAOFactory;

public class UserServiceImpl implements UserService {

	@Override
	public User insert(Map<String, Object> data) {
		String name = (String) data.get(ColumnIndexes.USER_NAME);
		String phone = (String) data.get(ColumnIndexes.USER_PHONE);
		String login = (String) data.get(ColumnIndexes.LOGIN);
		String password = (String) data.get(ColumnIndexes.PASSWORD);
		
		User user = new User(0L, name, login, password, phone);
		user = DAOFactory.getInstance().getUserDAO().insert(user);
		
		return user;
	}
	
	@Override
	public User modify(Map<String, Object> data){
		Long id = (Long) data.get(ColumnIndexes.USER_ID);
		String name = (String) data.get(ColumnIndexes.USER_NAME);
		String phone = (String) data.get(ColumnIndexes.USER_PHONE);
		String login = (String) data.get(ColumnIndexes.LOGIN);
		String password = (String) data.get(ColumnIndexes.PASSWORD);
		
		User user = new User(id, name, login, password, phone);
		user = DAOFactory.getInstance().getUserDAO().insert(user);
		
		return user;
	}
	
	@Override
	public void remove(Long id){
		DAOFactory.getInstance().getUserDAO().remove(id);
	}
	
	@Override
	public boolean alterPassword(String password, Long id){
		boolean altered = DAOFactory.getInstance().getUserDAO().alterPassword(password, id);
		
		return altered;
	}
	
	@Override
	public User login(String login, String password){
		User user = DAOFactory.getInstance().getUserDAO().login(login, password);
		
		return user;
	}
}
